package com.ruoyi.iot.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.domain.IotRegion;
import com.ruoyi.iot.service.IIotRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域Controller
 *
 * @author rww
 * @date 2023-11-02
 */
@RestController
@RequestMapping("/region/region")
public class IotRegionController extends BaseController
{
    @Autowired
    private IIotRegionService iotRegionService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询区域列表
     */
//    @PreAuthorize("@ss.hasPermi('region:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotRegion iotRegion, HttpServletRequest request)
    {
        startPage();
        Long userId = tokenService.getLoginUser(request).getUserId();
        iotRegion.setUserId(userId);
        List<IotRegion> list = iotRegionService.selectIotRegionList(iotRegion);
        return getDataTable(list);
    }

    @GetMapping("/list/all")
    public AjaxResult listAll(IotRegion iotRegion, HttpServletRequest request)
    {
        Long userId = tokenService.getLoginUser(request).getUserId();
        iotRegion.setUserId(userId);
        List<IotRegion> list = iotRegionService.selectIotRegionList(iotRegion);
        return AjaxResult.success(list);
    }

    /**
     * 导出区域列表
     */
//    @PreAuthorize("@ss.hasPermi('region:region:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotRegion iotRegion)
    {
        List<IotRegion> list = iotRegionService.selectIotRegionList(iotRegion);
        ExcelUtil<IotRegion> util = new ExcelUtil<IotRegion>(IotRegion.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
//    @PreAuthorize("@ss.hasPermi('region:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(iotRegionService.selectIotRegionById(id));
    }

    /**
     * 新增区域
     */
//    @PreAuthorize("@ss.hasPermi('region:region:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotRegion iotRegion, HttpServletRequest request)
    {
        Long userId = tokenService.getLoginUser(request).getUserId();
        iotRegion.setUserId(userId);
        return toAjax(iotRegionService.insertIotRegion(iotRegion));
    }

    /**
     * 修改区域
     */
//    @PreAuthorize("@ss.hasPermi('region:region:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotRegion iotRegion)
    {
        return toAjax(iotRegionService.updateIotRegion(iotRegion));
    }

    /**
     * 删除区域
     */
//    @PreAuthorize("@ss.hasPermi('region:region:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotRegionService.deleteIotRegionByIds(ids));
    }
}
